import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/admin/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: () => import('../views/Index')
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/home/about',
    children: [
      {
        path: 'about',
        name: 'About',
        component: () => import('../views/admin/About')
      }, {
        path: 'user',
        name: 'User',
        component: () => import('../views/admin/user/UserList')
      }, {
        path: 'banner',
        name: 'Banner',
        component: () => import('../views/admin/banner/BannerList')
      }, {
        path: 'href',
        name: 'Href',
        component: () => import('../views/admin/href/HrefList')
      }, {
        path: 'website',
        name: 'Website',
        component: () => import('../views/admin/website/Website')
      }, {
        path: 'category',
        name: 'Category',
        component: () => import('../views/admin/category/CategoryList')
      }, {
        path: 'apply',
        name: 'UpdateArticle',
        component: () => import('../views/admin/article/ArticleUpdate')
      }, {
        path: 'article',
        name: 'Article',
        component: () => import('../views/admin/article/ArticleList')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/admin/Login')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path.match('home')) {
    if (Vue.$cookies.get('token')) {
      next()
    } else {
      Vue.prototype.$alert('无权访问，请先登录！', { type: 'error' }).then(() => {
        next({ path: '/login' })
      })
    }
  } else {
    next()
  }
})

export default router
